﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GetAStringDemo
{
    public class Currency
    {
        public uint Dollars;//美元
        public ushort Cents;//美分
        public Currency(uint dollers, ushort cents)
        {
            Dollars = dollers;
            Cents = cents;
        }

        public override string ToString() => $"${Dollars}.{Cents,2:00}";
        //{
        //    return base.ToString();
        //}
        public void Totring()
        {
            Console.WriteLine($"${Dollars}.{Cents,2:00}");
        }

        public static string GetCurrencyUnit() => "Dollar";//当前货币单位

        public static explicit operator Currency(float value)
        {
            checked
            {
                uint dollars = (uint)value;
                ushort cents = (ushort)((value - dollars) * 100);
                return new Currency(dollars, cents);

            }
        }

        public static implicit operator float(Currency value) =>
         value.Dollars + (value.Cents / 100.0f);


        public static implicit operator Currency(uint value) =>
          new Currency(value, 0);


        public static implicit operator uint(Currency value) =>
          value.Dollars;
    }

}
